package action;


import model.SessionInfo;
import model.Trole;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import pageModel.DataGrid;
import pageModel.Json;
import pageModel.User;
import service.UserServiceI;
import util.ConfigUtil;
import util.MD5Util;

import com.opensymphony.xwork2.ModelDriven;

@Namespace("/")
@Action(value="userAction",results = { @Result(name = "userEdit", location = "/admin/userEdit.jsp")})
public class UserAction extends BaseAction implements ModelDriven<User>{
	
	private UserServiceI userService;
	User user=new User();

public UserServiceI getUserService() {
		return userService;
	}
@Override
public User getModel() {
	return user;
}
@Autowired
public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}

public String userEdit() {
	return "userEdit";
}

public void reg(){
	Json j=new Json();
	try{
		userService.save(user);
		j.setSuccess(true);
		j.setMsg("注册成功!");
	}catch(Exception e){
		j.setMsg("注册失败,用户名已被注册!");
	}
	super.writeJson(j);
}
public void log(){ 
	Json j=new Json();
	User u=userService.findByLogname(user.getLogname()); 
    try {
      // 如果登陆成功
      if (u.getLogname().equals(user.getLogname()) && u.getPwd().equals(MD5Util.md5(user.getPwd()))){
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLogname(), MD5Util.md5(user.getPwd()).toString());
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        subject.hasRole("店长");
        j.setSuccess(true);
        j.setMsg("登录成功!");
        SessionInfo sessionInfo = new SessionInfo();
		sessionInfo.setUser(u);
		getSession().setAttribute(ConfigUtil.getSessionInfoName(), sessionInfo);
		j.setObj(sessionInfo);
      }else{
    	  j.setSuccess(false);
          j.setMsg("登录失败,用户名或密码错误!"); 
      }
    } catch (Exception e) {
    	 j.setMsg("登录失败,用户名或密码错误!");
    }
	super.writeJson(j);
	}
public void logout(){
		if (getSession() != null) {
			getSession().invalidate();
		}
		Json j = new Json();
		j.setSuccess(true);
		writeJson(j);
}
public void datagrid(){
	super.writeJson(userService.datagrid(user));
}
public void add(){
	Json j=new Json();
	try{
		userService.save(user);
		j.setSuccess(true);
		j.setMsg("添加成功!");
	}catch(Exception e){
		j.setMsg("添加失败,该用户在数据库中已存在!");
	}
	super.writeJson(j);
}
public void del(){
	Json j=new Json();
	try{
	userService.remove(user.getIds());
	j.setSuccess(true);
	j.setMsg("删除成功!");
  }catch(Exception e){
	j.setMsg("删除失败!"); 
  }
	super.writeJson(j);
}
public void editor(){
	Json j=new Json();
	try{
	userService.edit(user);
	j.setSuccess(true);
	j.setMsg("编辑成功!");
  }catch(Exception e){
	j.setMsg("编辑失败!"); 
  }
	super.writeJson(j);
}
public void editPwd(){
	Json j=new Json();
	try{
	userService.editPwd(user);
	j.setSuccess(true);
	j.setMsg("修改密码成功,下次登录时生效!");
	}catch(Exception e){
	j.setMsg("原密码输入错误或输入的新密码与原密码相同!");	
	}
	super.writeJson(j);
}
}
